/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "stdafx.h"
#include "线管绘制.h"



/*VkRenderPass f_创建渲染通道(VkDevice& dev, const VkFormat& 颜色格式, const VkFormat& 深度格式) {
	VkAttachmentDescription 附件描述[2];
	附件描述[0] = f_创建附件描述(颜色格式);
	附件描述[1] = f_创建附件描述(深度格式, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);




	VkAttachmentReference 颜色附件引用 = {};
	颜色附件引用.attachment = 0;
	颜色附件引用.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;

	VkAttachmentReference 深度附件引用 = {};
	深度附件引用.attachment = 1;
	深度附件引用.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;


	VkSubpassDescription 子通道 = {};
	子通道.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
	子通道.flags = 0;
	子通道.inputAttachmentCount = 0;
	子通道.pInputAttachments = NULL;

	
	子通道.pResolveAttachments = NULL;
	子通道.preserveAttachmentCount = 0;
	子通道.pPreserveAttachments = NULL;

	子通道.colorAttachmentCount = 1;
	子通道.pColorAttachments = &颜色附件引用;
	子通道.pDepthStencilAttachment = &深度附件引用;


	VkSubpassDependency 附属 = {};
	附属.srcSubpass = VK_SUBPASS_EXTERNAL;
	附属.dstSubpass = 0;
	附属.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
	附属.srcAccessMask = 0;
	附属.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
	附属.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;







	VkRenderPassCreateInfo rpInfo = {};

	rpInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
	rpInfo.pNext = NULL;

	rpInfo.attachmentCount = 2;
	rpInfo.pAttachments = 附件描述;

	rpInfo.subpassCount = 1;
	rpInfo.pSubpasses = &子通道;

	rpInfo.dependencyCount = 0;
	rpInfo.pDependencies = 0;

	


	VkRenderPass 渲染通道;
	VkResult res = vkCreateRenderPass(dev, &rpInfo, NULL, &渲染通道);
	if (res != VK_SUCCESS) {
		throw std::runtime_error("创建渲染过程失败");
	}

	

	return 渲染通道;

}*/





void f_Vk绘制(VkDevice& dev) {
	/*VkClearValue 数据清除[2];
	数据清除[0].color.float32[0] = 1.0f;
	数据清除[0].color.float32[1] = 0.0f;
	数据清除[0].color.float32[2] = 0.0f;
	数据清除[0].color.float32[3] = 0.0f;
	数据清除[1].depthStencil.depth = 1.0;
	数据清除[1].depthStencil.stencil = 0;

	VkSemaphore 图像信号;
	VkSemaphoreCreateInfo 图像信号创建信息;

	图像信号创建信息.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
	图像信号创建信息.pNext = 0;
	图像信号创建信息.flags = 0;

	VkResult res;
	res = vkCreateSemaphore(dev, &图像信号创建信息, NULL, &图像信号);
	assert(res == VK_SUCCESS);*/




}










VkAttachmentDescription f_创建附件描述(const VkFormat& 格式, VkImageLayout 图像布局类型, VkSampleCountFlagBits 采用次数) {
	VkAttachmentDescription 附件描述;
	附件描述.format = 格式;
	附件描述.samples = 采用次数;
	附件描述.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;					//子通道开始的操作  针对颜色附件
	附件描述.storeOp = VK_ATTACHMENT_STORE_OP_STORE;				//子通道结束的操作  针对颜色附件
	附件描述.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
	附件描述.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
	附件描述.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
	附件描述.finalLayout = 图像布局类型;
	附件描述.flags = 0;

	/*附件[1].format = info.depth.format;
	附件[1].samples = 采用次数;
	附件[1].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
	附件[1].storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
	附件[1].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
	附件[1].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
	附件[1].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
	附件[1].finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
	附件[1].flags = 0;*/
	return 附件描述;
}




VkRenderPass f_创建渲染通道(VkDevice& dev, const VkFormat& 颜色格式, const VkFormat& 深度格式, E_MS次数 采样次数) {
	VkAttachmentDescription 附件描述[4];
	//附件描述[0] = f_创建附件描述(颜色格式, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, f_get多重采样Bits(采样次数));
	//附件描述[1] = f_创建附件描述(颜色格式, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, VK_SAMPLE_COUNT_1_BIT);

	//附件描述[2] = f_创建附件描述(深度格式, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, f_get多重采样Bits(采样次数));
	//附件描述[3] = f_创建附件描述(深度格式, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, VK_SAMPLE_COUNT_1_BIT);

	附件描述[0].format = 颜色格式; //设置用于多重采样的颜色附件格式
	附件描述[0].samples = f_get多重采样Bits(采样次数); //设置多重采样数
	附件描述[0].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;//渲染通道开始时颜色附件的操作
	附件描述[0].storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;//渲染通道结束时颜色附件的操作
	附件描述[0].finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; //颜色附件的最终布局
	附件描述[0].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
	附件描述[0].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
	附件描述[0].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
	附件描述[0].flags = 0;

	附件描述[1].format = 颜色格式; //设置解析附件的格式(服务于呈现)
	附件描述[1].samples = VK_SAMPLE_COUNT_1_BIT; //设置多重采样数
	附件描述[1].loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;//渲染通道开始时解析附件的操作
	附件描述[1].storeOp = VK_ATTACHMENT_STORE_OP_STORE; //渲染通道结束时解析附件的操作
	附件描述[1].finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR; //解析附件的最终布局
	附件描述[1].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
	附件描述[1].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
	附件描述[1].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
	附件描述[1].flags = 0;

	附件描述[2].format = 深度格式; //设置用于多重采样的深度附件格式
	附件描述[2].samples = f_get多重采样Bits(采样次数); //设置多重采样数
	附件描述[2].finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; //用于多重采样的深度附件的最终布局
	附件描述[2].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
	附件描述[2].storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
	附件描述[2].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
	附件描述[2].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
	附件描述[2].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
	附件描述[2].flags = 0;

	附件描述[3].format = 深度格式;
	附件描述[3].samples = VK_SAMPLE_COUNT_1_BIT;
	附件描述[3].loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
	附件描述[3].storeOp = VK_ATTACHMENT_STORE_OP_STORE;
	附件描述[3].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
	附件描述[3].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
	附件描述[3].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
	附件描述[3].finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
	附件描述[3].flags = 0;





	VkAttachmentReference 颜色附件引用 = {};
	颜色附件引用.attachment = 0;
	颜色附件引用.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;

	VkAttachmentReference 深度附件引用 = {};
	深度附件引用.attachment = 2;
	深度附件引用.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;

	VkAttachmentReference 解析附件引用 = {};
	解析附件引用.attachment = 1;
	解析附件引用.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;


	VkSubpassDescription 子通道 = {};
	子通道.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
	子通道.flags = 0;
	子通道.inputAttachmentCount = 0;
	子通道.pInputAttachments = NULL;


	子通道.pResolveAttachments = &解析附件引用;
	子通道.preserveAttachmentCount = 0;
	子通道.pPreserveAttachments = NULL;

	子通道.colorAttachmentCount = 1;
	子通道.pColorAttachments = &颜色附件引用;
	子通道.pDepthStencilAttachment = &深度附件引用;




	VkSubpassDependency 附属[2] = {};
	附属[0].srcSubpass = VK_SUBPASS_EXTERNAL;
	附属[0].dstSubpass = 0;

	附属[0].srcStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
	附属[0].dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;

	附属[0].srcAccessMask = VK_ACCESS_MEMORY_READ_BIT;
	附属[0].dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
	附属[0].dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT;


	附属[1].srcSubpass = 0;
	附属[1].dstStageMask = VK_SUBPASS_EXTERNAL;

	附属[1].srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
	附属[1].dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;

	附属[1].dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
	附属[1].srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
	附属[1].dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT;





	VkRenderPassCreateInfo rpInfo = {};

	rpInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
	rpInfo.pNext = NULL;

	rpInfo.attachmentCount = 4;
	rpInfo.pAttachments = 附件描述;

	rpInfo.subpassCount = 1;
	rpInfo.pSubpasses = &子通道;

	rpInfo.dependencyCount = 2;
	rpInfo.pDependencies = 附属;




	VkRenderPass 渲染通道;
	VkResult res = vkCreateRenderPass(dev, &rpInfo, NULL, &渲染通道);
	if (res != VK_SUCCESS) {
		throw std::runtime_error("创建渲染过程失败");
	}



	return 渲染通道;

}

VkRenderPass f_创建纹理渲染通道(VkDevice& dev, const VkFormat& 颜色格式, const VkFormat& 深度格式, E_MS次数 采样次数) {
	VkAttachmentDescription 附件描述[4];


	附件描述[0].format = 颜色格式; //设置用于多重采样的颜色附件格式
	附件描述[0].samples = f_get多重采样Bits(采样次数); //设置多重采样数
	附件描述[0].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;						//渲染通道开始时颜色附件的操作
	附件描述[0].storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;					//渲染通道结束时颜色附件的操作
	附件描述[0].finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;		//颜色附件的最终布局
	附件描述[0].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
	附件描述[0].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
	附件描述[0].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
	附件描述[0].flags = 0;

	附件描述[1].format = 颜色格式;											//设置解析附件的格式(服务于呈现)
	附件描述[1].samples = VK_SAMPLE_COUNT_1_BIT;							//设置多重采样数
	附件描述[1].loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;					//渲染通道开始时解析附件的操作
	附件描述[1].storeOp = VK_ATTACHMENT_STORE_OP_STORE;						//渲染通道结束时解析附件的操作
	附件描述[1].finalLayout = VK_IMAGE_LAYOUT_GENERAL;						//解析附件的最终布局
	附件描述[1].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
	附件描述[1].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
	附件描述[1].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
	附件描述[1].flags = 0;

	附件描述[2].format = 深度格式; //设置用于多重采样的深度附件格式
	附件描述[2].samples = f_get多重采样Bits(采样次数); //设置多重采样数
	附件描述[2].finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; //用于多重采样的深度附件的最终布局
	附件描述[2].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
	附件描述[2].storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
	附件描述[2].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
	附件描述[2].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
	附件描述[2].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
	附件描述[2].flags = 0;

	附件描述[3].format = 深度格式;
	附件描述[3].samples = VK_SAMPLE_COUNT_1_BIT;
	附件描述[3].loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
	附件描述[3].storeOp = VK_ATTACHMENT_STORE_OP_STORE;
	附件描述[3].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
	附件描述[3].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
	附件描述[3].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
	附件描述[3].finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
	附件描述[3].flags = 0;





	VkAttachmentReference 颜色附件引用 = {};
	颜色附件引用.attachment = 0;
	颜色附件引用.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;

	VkAttachmentReference 深度附件引用 = {};
	深度附件引用.attachment = 2;
	深度附件引用.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;

	VkAttachmentReference 解析附件引用 = {};
	解析附件引用.attachment = 1;
	解析附件引用.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;


	VkSubpassDescription 子通道 = {};
	子通道.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
	子通道.flags = 0;
	子通道.inputAttachmentCount = 0;
	子通道.pInputAttachments = NULL;


	子通道.pResolveAttachments = &解析附件引用;
	子通道.preserveAttachmentCount = 0;
	子通道.pPreserveAttachments = NULL;

	子通道.colorAttachmentCount = 1;
	子通道.pColorAttachments = &颜色附件引用;
	子通道.pDepthStencilAttachment = &深度附件引用;




	VkSubpassDependency 附属[2] = {};
	附属[0].srcSubpass = VK_SUBPASS_EXTERNAL;
	附属[0].dstSubpass = 0;

	附属[0].srcStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
	附属[0].dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;

	附属[0].srcAccessMask = VK_ACCESS_MEMORY_READ_BIT;
	附属[0].dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
	附属[0].dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT;


	附属[1].srcSubpass = 0;
	附属[1].dstStageMask = VK_SUBPASS_EXTERNAL;

	附属[1].srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
	附属[1].dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;

	附属[1].dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
	附属[1].srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
	附属[1].dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT;





	VkRenderPassCreateInfo rpInfo = {};

	rpInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
	rpInfo.pNext = NULL;

	rpInfo.attachmentCount = 4;
	rpInfo.pAttachments = 附件描述;

	rpInfo.subpassCount = 1;
	rpInfo.pSubpasses = &子通道;

	rpInfo.dependencyCount = 2;
	rpInfo.pDependencies = 附属;




	VkRenderPass 渲染通道;
	VkResult res = vkCreateRenderPass(dev, &rpInfo, NULL, &渲染通道);
	if (res != VK_SUCCESS) {
		throw std::runtime_error("创建渲染过程失败");
	}



	return 渲染通道;
}





